package com.noble.meether.db.type.msg;

import android.content.ContentValues;
import android.database.Cursor;
import android.text.TextUtils;

import com.noble.meether.chat.msgtype.base.BaseMessage;
import com.noble.meether.db.utils.CursorUtil;

import rx.functions.Func1;

/**
 * 数据库：消息表
 */
public class MsgTable {
    public static final String TABLE_NAME = "message";   // 表名

    // 消息体字段
    public static final String COLUMN_UID_FROM = "UidFrom";       // 私聊ID： uidFrom
    public static final String COLUMN_UID_TO = "UidTo";            // 发送ID:  uidTo
    public static final String COLUMN_TYPE = "TypeMsg";            // 消息类型
    public static final String COLUMN_CONTENT = "Content";          //json

    // 自定义字段
    public static final String ID = "id";                         // 自增id
    public static final String COLUMN_IS_SENDER = "Sender";         // 自己： 是否是发送者  = 1: 是发送者
    public static final String COLUMN_WHISPER = "Whisper";          // 聊天对象ID
    public static final String COLUMN_SEND_STATUS = "sendStatus";    // 消息发送状态； = 1： 发送失败

    // 建表语句
    public static String getCreateTable() {
        return "CREATE TABLE IF NOT EXISTS " + TABLE_NAME + "(" +
                ID + " INTEGER PRIMARY KEY AUTOINCREMENT," +
                COLUMN_IS_SENDER + " INT," +
                COLUMN_SEND_STATUS + " INT," +
                COLUMN_WHISPER + " INTEGER NOT NULL," +
                COLUMN_UID_FROM + " INTEGER NOT NULL," +
                COLUMN_UID_TO + " INTEGER NOT NULL," +
                COLUMN_TYPE + " INTEGER NOT NULL," +
                COLUMN_CONTENT + " TEXT);";
    }

    // 根据表中的row生成一个对象
    public static Func1<Cursor, BaseMessage> MAPPER = cursor -> {
        BaseMessage data = new BaseMessage();
        data.setId(CursorUtil.getInt(cursor, ID));
        data.setIsSender(CursorUtil.getInt(cursor, COLUMN_IS_SENDER));
        data.setWhisperId(CursorUtil.getLong(cursor, COLUMN_WHISPER));
        data.setSendStatus(CursorUtil.getInt(cursor, COLUMN_SEND_STATUS));

        data.setUidFrom(CursorUtil.getLong(cursor, COLUMN_UID_FROM));
        data.setUidTo(CursorUtil.getLong(cursor, COLUMN_UID_TO));
        data.setMsgType(CursorUtil.getInt(cursor, COLUMN_TYPE));
        data.setContent(CursorUtil.getString(cursor, COLUMN_CONTENT));
        return data;
    };

    // 根据对象生成一条数据库记录
    public static ContentValues toContentValues(BaseMessage data) {
        ContentValues values = new ContentValues();
        values.put(COLUMN_IS_SENDER, data.getIsSender());
        values.put(COLUMN_SEND_STATUS, data.getSendStatus());
        if (data.getWhisperId() > 0) {
            values.put(COLUMN_WHISPER, data.getWhisperId());
        }

        if (data.getLuidFrom() > 0)
            values.put(COLUMN_UID_FROM, data.getLuidFrom());
        if (data.getUidTo() > 0)
            values.put(COLUMN_UID_TO, data.getUidTo());
        if (data.getMsgType() > 0)
            values.put(COLUMN_TYPE, data.getMsgType());
        if (!TextUtils.isEmpty(data.getContent()))
            values.put(COLUMN_CONTENT, data.getContent());
        return values;
    }


}
